﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace proyecto
{
    /// <summary>
    /// Lógica de interacción para SelectARGS.xaml
    /// </summary>
    public partial class SelectARGS : Window
    {


        DockPanel[] panels;
        ComboBox[] combos;
        Label[] labels;

        bool ok;


        public SelectARGS()
        {
            InitializeComponent();
        }

        public SelectARGS(string[] args, int argCount)
        {

            InitializeComponent();

            ok = false;

            panels = new DockPanel[argCount];
            combos = new ComboBox[argCount];
            labels = new Label[argCount];

            for (int i = 0; i < argCount; i++)
            {

                panels[i] = new DockPanel();
                panels[i].Height = 40;
                panels[i].LastChildFill = false;

                combos[i] = new ComboBox();
                combos[i].Width = 120;
                ComboBox Auxcombo = new ComboBox();
                if (i == 0)
                {
                    combos[i].Items.Add("Input Table");
                    combos[i].SelectedIndex = 0;
                    //combos[i].IsEnabled = false;

                    ComboBox combo = new ComboBox();

                    for (int j = 0; j < 20; j++)
                        if (Window1.dat[0].isCreated(j))
                            combo.Items.Add(Window1.dat[0].getTableName(j));

                    combo.SelectedIndex = 0;

                    combo.Width = 120;
                    DockPanel.SetDock(combo, Dock.Right);
                    panels[i].Children.Add(combo);
                }
                else
                    if (i == 1)
                    {
                        combos[i].Items.Add("Output File");
                        combos[i].SelectedIndex = 0;
                        //combos[i].IsEnabled = false;

                        
                    }
                    else
                    {
                        combos[i].Items.Add("Data Table (as file)");
                        combos[i].Items.Add("Int");
                        combos[i].Items.Add("Double");
                        combos[i].Items.Add("String");
                        combos[i].Name = "combo"+i.ToString();
                        combos[i].SelectedIndex = 0;
                        combos[i].SelectionChanged += new SelectionChangedEventHandler(ChangedCombo);                   

                        for (int j = 0; j < 20; j++)
                            if (Window1.dat[0].isCreated(j))
                                Auxcombo.Items.Add(Window1.dat[0].getTableName(j));

                        Auxcombo.SelectedIndex = 0;

                        Auxcombo.Width = 120;
                        //Auxcombo.Name = "AUX";
                        
                    
                    }

                labels[i] = new Label();
                labels[i].Content = args[i];
                labels[i].Width = 120;

                DockPanel.SetDock(panels[i], Dock.Top);
                DockPanel.SetDock(labels[i], Dock.Left);
                DockPanel.SetDock(combos[i], Dock.Left);

                panels[i].Children.Add(labels[i]);
                panels[i].Children.Add(combos[i]);

                /*if (i > 1)
                {
                    DockPanel.SetDock(Auxcombo, Dock.Right);
                    panels[i].Children.Add(Auxcombo);
                }*/

                dockPanel3.Children.Add(panels[i]);
            }

        }

        private void ChangedCombo(object sender, SelectionChangedEventArgs arg)
        {

            int i = Convert.ToInt16((sender as ComboBox).Name.Replace("combo", ""));

            if (panels[i].Children.Count>2)
                panels[i].Children.Remove(panels[i].Children[2]);

            if (combos[i].SelectedIndex == 0)
            {

                ComboBox combo = new ComboBox();

                for (int j = 0; j < Window1.dat[0].getNtables(); j++)

                    combo.Items.Add(Window1.dat[0].getTableName(j));

                combo.SelectedIndex = 0;

                combo.Width = 120;

                DockPanel.SetDock(combo, Dock.Right);
                panels[i].Children.Add(combo);


            }

            else
            {

                TextBox text = new TextBox();

                text.Width = 120;

                DockPanel.SetDock(text, Dock.Right);
                panels[i].Children.Add(text);
            }


        }


        public int getArgType(int i)
        {

            if (i == 0 || i == 1)
                return i;

            else
                if (combos[i].SelectedIndex == 0) return 0;
                else
                    return (combos[i].SelectedIndex + 1);

        }

        public int getIntValue(int i)
        {

            return Convert.ToInt16((panels[i].Children[2] as TextBox).Text);
        }

        public double getDoubleValue(int i)
        {

            return Convert.ToDouble((panels[i].Children[2] as TextBox).Text);
        }

        public string getStringValue(int i)
        {

            return ((panels[i].Children[2] as TextBox).Text);
        }

        public int getTableValue(int i)
        {

            return (panels[i].Children[2] as ComboBox).SelectedIndex;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ok = true;
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public bool getOK()
        {
            return ok;
        }

        public bool getTranspose()
        {

            return checkBox1.IsChecked.Value;
        }






    }
}
